from datetime import datetime

from django.db import models
from django.utils import timezone


class User(models.Model):
    GENDER_CHOICES = (
        ('M', '男'),
        ('F', '女'),
    )
    ROLE_CHOICES = (
        ('0', '管理员'),
        ('1', '普通用户'),
    )
    STATUS_CHOICES = (
        ('0', '正常'),
        ('1', '封号'),
    )
    username = models.CharField(max_length=50, null=True)
    password = models.CharField(max_length=256, null=True)
    cover = models.ImageField(upload_to='cover/', null=True, default="cover/default.jpg", verbose_name="头像")
    role = models.CharField(max_length=2, blank=True, null=True)
    status = models.CharField(max_length=1, choices=STATUS_CHOICES, default='0')
    nickname = models.CharField(blank=True, null=True, max_length=20)
    avatar = models.FileField(upload_to='avatar/', null=True)
    mobile = models.CharField(max_length=13, blank=True, null=True)
    email = models.CharField(max_length=50, blank=True, null=True)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True, null=True)
    description = models.TextField(max_length=200, null=True)
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    score = models.IntegerField(default=0, blank=True, null=True)
    push_email = models.CharField(max_length=40, blank=True, null=True)
    push_switch = models.BooleanField(blank=True, null=True, default=False)

    class Meta:
        db_table = "user"
        verbose_name = "用户表"
        verbose_name_plural = verbose_name


class LoginLog(models.Model):
    username = models.CharField(max_length=50, blank=True, null=True)
    ip = models.CharField(max_length=100, blank=True, null=True)
    ua = models.CharField(max_length=200, blank=True, null=True)
    log_time = models.DateTimeField(auto_now_add=True, null=True)

    class Meta:
        db_table = "login_log"


# 操作日志
class OpLog(models.Model):
    re_ip = models.CharField(max_length=100, blank=True, null=True)
    # 创建时间
    re_time = models.DateTimeField(auto_now_add=True, null=True)
    re_url = models.CharField(max_length=200, blank=True, null=True)
    re_method = models.CharField(max_length=10, blank=True, null=True)
    re_ua = models.CharField(max_length=200, blank=True, null=True)
    # 耗时
    access_time = models.CharField(max_length=20, blank=True, null=True)

    class Meta:
        db_table = "op_log"
        verbose_name = "操作日志"
        verbose_name_plural = verbose_name


class ErrorLog(models.Model):
    ip = models.CharField(max_length=100, blank=True, null=True)
    url = models.CharField(max_length=200, blank=True, null=True)
    method = models.CharField(max_length=10, blank=True, null=True)
    content = models.CharField(max_length=200, blank=True, null=True)
    log_time = models.DateTimeField(auto_now_add=True, null=True)

    class Meta:
        db_table = "error_log"
        verbose_name = "错误日志"
        verbose_name_plural = verbose_name


# 服务模型
class Classification(models.Model):
    title = models.CharField(max_length=100, blank=True, null=True)
    create_time = models.DateTimeField(auto_now_add=True, null=True)

    def __str__(self):
        return self.title

    class Meta:
        db_table = "classification"
        verbose_name = "分类表"
        verbose_name_plural = verbose_name


class Tag(models.Model):
    title = models.CharField(max_length=100, blank=True, null=True)
    create_time = models.DateTimeField(auto_now_add=True, null=True)

    class Meta:
        db_table = "tag"
        verbose_name = "标签表"
        verbose_name_plural = verbose_name


# 家政表
class HouseKeeping(models.Model):
    STATUS_CHOICES = (
        ('0', '上架'),
        ('1', '下架'),
    )
    id = models.BigAutoField(primary_key=True)
    classification = models.ForeignKey(Classification, on_delete=models.CASCADE, blank=True, null=True,
                                       related_name='classification_thing')
    user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True, related_name='user_thing')
    tag = models.ManyToManyField(Tag, related_name='thing_set', blank=True)  # 标签 ?

    title = models.CharField(max_length=100, blank=True, null=True, verbose_name="姓名")
    cover = models.ImageField(upload_to='cover/', null=True, default="cover/default.jpg", verbose_name="头像")
    description = models.TextField(max_length=1000, blank=True, null=True, verbose_name="个人简介")
    price = models.FloatField(max_length=50, blank=True, null=True, verbose_name="小时价格")
    mobile = models.CharField(max_length=50, blank=True, null=True, verbose_name="手机号")
    age = models.CharField(max_length=10, blank=True, null=True, verbose_name="年龄")
    sex = models.CharField(max_length=2, blank=True, null=True, verbose_name="性别")
    location = models.CharField(max_length=50, blank=True, null=True, verbose_name="地区")
    status = models.CharField(max_length=1, choices=STATUS_CHOICES, default='0')  # 上架下架
    # 更新时间
    create_time = models.DateTimeField(auto_now=True, null=True, verbose_name="更新时间")
    pv = models.IntegerField(default=0, verbose_name="访问量")
    recommend_count = models.IntegerField(default=0)  # 评论 ?
    wish = models.ManyToManyField(User, blank=True, related_name="wish_things")
    wish_count = models.IntegerField(default=0, verbose_name="心愿单数量")
    collect = models.ManyToManyField(User, blank=True, related_name="collect_things")
    collect_count = models.IntegerField(default=0, verbose_name="收藏数量")

    class Meta:
        db_table = "thing"


class Comment(models.Model):
    content = models.CharField(max_length=200, blank=True, null=True)
    # 用户和评论一对多
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, related_name="user_comment")
    # 家政和评论一对多
    thing = models.ForeignKey(HouseKeeping, on_delete=models.CASCADE, null=True, related_name="thing_comment")
    comment_time = models.DateTimeField(auto_now=True, null=True)
    like_count = models.IntegerField(default=0)
    
    class Meta:
        db_table = "comment"
        verbose_name = "评论表"
        verbose_name_plural = verbose_name


class Address(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, related_name='user_address')
    name = models.CharField(max_length=100, blank=True, null=True)
    address = models.CharField(max_length=100, blank=True, null=True)
    mobile = models.CharField(max_length=30, blank=True, null=True)
    default = models.BooleanField(blank=True, null=True, default=False, verbose_name="默认地址")  # 是否默认地址
    create_time = models.DateTimeField(auto_now_add=True, null=True)

    class Meta:
        db_table = "address"
        verbose_name = "地址表"
        verbose_name_plural = verbose_name


# 订单模型
class Order(models.Model):
    out_trade_no = models.CharField(max_length=64, unique=True, verbose_name="订单号", null=True)
    # status_choices = [
    #     (0, "未支付"),
    #     (1, "已支付"),
    #     (2, "订单取消")
    # ]
    pay_choices = (
        (1, '支付宝'),
        (2, '微信支付'),
    )
    status = models.CharField(default=2, max_length=2, blank=True, null=True)  # 1未支付 2已支付 3订单取消
    subject = models.CharField(max_length=150, verbose_name="订单标题", null=True)
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="订单总价", default=0)
    trade_no = models.CharField(max_length=64, null=True, verbose_name="流水号")
    pay_type = models.SmallIntegerField(choices=pay_choices, default=1, verbose_name="支付方式")
    order_time = models.DateTimeField(auto_now_add=True, verbose_name="订单创建时间")
    # 支付宝会返回支付时间
    pay_time = models.DateTimeField(verbose_name="订单支付时间",default=timezone.now)
    count = models.PositiveIntegerField(default=0, verbose_name="数量")
    # 外键字段
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='user', verbose_name='下单用户')
    housekeeping = models.ForeignKey(HouseKeeping, related_name='housekeeping_order', on_delete=models.CASCADE,
                                     null=True, verbose_name="家政人员")
    # 下单时间
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', null=True)

    # def __str__(self):
    #     return "%s - ￥%s" % (self.house_keeping.title, self.total_amount)

    class Meta:
        db_table = 'order'
        verbose_name = '订单记录'
        verbose_name_plural = verbose_name


# 订单详情表
class OrderDetail(models.Model):
    # 一个订单详情跟订单一对多，关联字段写在多的一方

    # 课程和订单详情，一对多，一个课程，可以对应多个订单详情
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, verbose_name='下单用户')
    housekeeping = models.ForeignKey(HouseKeeping, on_delete=models.SET_NULL, null=True,
                                     verbose_name="家政人员")
    count = models.PositiveIntegerField(default=0, verbose_name="数量")
    address = models.ForeignKey(Address, related_name='address_orders', on_delete=models.SET_NULL,
                                null=True, verbose_name="该用户填写得地址")
    receiver_name = models.CharField(max_length=100, blank=True, null=True)
    receiver_address = models.CharField(max_length=100, blank=True, null=True)
    receiver_phone = models.CharField(max_length=30, blank=True, null=True)
    price = models.FloatField(max_length=50, blank=True, null=True, verbose_name="小时价格")
    title= models.CharField(max_length=300, blank=True, null=True)
    remark = models.CharField(max_length=300, blank=True, null=True)
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="订单总价", default=0)
    order = models.ForeignKey(Order, related_name='order_housekeeping', on_delete=models.CASCADE,
                              verbose_name="订单", default=None,null=True)
    class Meta:
        db_table = "order_detail"
        verbose_name = "订单详情"
        verbose_name_plural = verbose_name

    # def __str__(self):
    #     try:
    #         return "%s的订单：%s" % (self.housekeeping.title, self.order.out_trade_no)
    #     except:
    #         return super().__str__()


class OrderLog(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, related_name='user_order_log')
    thing = models.ForeignKey(HouseKeeping, on_delete=models.CASCADE, null=True, related_name='thing_order_log')
    action = models.CharField(max_length=2, blank=True, null=True)
    log_time = models.DateTimeField(auto_now_add=True, null=True)

    class Meta:
        db_table = "order_log"
        verbose_name = '订单日志'
        verbose_name_plural = verbose_name


# 通知表
class Notice(models.Model):
    title = models.CharField(max_length=100, blank=True, null=True)
    content = models.CharField(max_length=1000, blank=True, null=True)
    create_time = models.DateTimeField(auto_now_add=True, null=True)

    class Meta:
        db_table = "notice"
        verbose_name = "广告通知表"
        verbose_name_plural = verbose_name
